package org.jeecg.modules.maintain.controller;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.maintain.entity.CamelliaDeviceRepair;
import org.jeecg.modules.maintain.entity.CamelliaDeviceReport;
import org.jeecg.modules.maintain.entity.SysUser;
import org.jeecg.modules.maintain.service.ICamelliaDeviceRepairRelationshiopService;
import org.jeecg.modules.maintain.service.ICamelliaDeviceRepairService;
import org.jeecg.modules.maintain.service.ICamelliaDeviceReportService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @Description: 设备维修表
 * @Author: jeecg-boot
 * @Date:   2022-12-14
 * @Version: V1.0
 */
@Api(tags="设备维修表")
@RestController
@RequestMapping("/camelliaDeviceRepairs/camelliaDeviceRepair")
@Slf4j
public class CamelliaDeviceRepairController extends JeecgController<CamelliaDeviceRepair, ICamelliaDeviceRepairService> {
	@Autowired
	private ICamelliaDeviceRepairService camelliaDeviceRepairService;

	@Autowired
	private ICamelliaDeviceRepairRelationshiopService camelliaDeviceRepairRelationshiopService;


	@Autowired
	private ICamelliaDeviceReportService camelliaDeviceReportService;
	/**
	 * 分页列表查询
	 *
	 * @param camelliaDeviceRepair
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "设备维修表-分页列表查询")
	@ApiOperation(value="设备维修表-分页列表查询", notes="设备维修表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<CamelliaDeviceRepair>> queryPageList(CamelliaDeviceRepair camelliaDeviceRepair,
															 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															 HttpServletRequest req) {
		QueryWrapper<CamelliaDeviceRepair> queryWrapper = QueryGenerator.initQueryWrapper(camelliaDeviceRepair, req.getParameterMap());
		Page<CamelliaDeviceRepair> page = new Page<CamelliaDeviceRepair>(pageNo, pageSize);
		IPage<CamelliaDeviceRepair> pageList = camelliaDeviceRepairService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param camelliaDeviceRepair
	 * @return
	 */
	@AutoLog(value = "设备维修表-添加")
	@ApiOperation(value="设备维修表-添加", notes="设备维修表-添加")
	//@RequiresPermissions("camelliaDeviceRepairs:camellia_device_repair:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody CamelliaDeviceRepair camelliaDeviceRepair) {
		camelliaDeviceRepairService.save(camelliaDeviceRepair);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param camelliaDeviceRepair
	 * @return
	 */
	@AutoLog(value = "设备维修表-编辑")
	@ApiOperation(value="设备维修表-编辑", notes="设备维修表-编辑")
	//@RequiresPermissions("camelliaDeviceRepairs:camellia_device_repair:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody CamelliaDeviceRepair camelliaDeviceRepair) {
		camelliaDeviceRepairService.updateById(camelliaDeviceRepair);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备维修表-通过id删除")
	@ApiOperation(value="设备维修表-通过id删除", notes="设备维修表-通过id删除")
	//@RequiresPermissions("camelliaDeviceRepairs:camellia_device_repair:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		camelliaDeviceRepairService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备维修表-批量删除")
	@ApiOperation(value="设备维修表-批量删除", notes="设备维修表-批量删除")
	//@RequiresPermissions("camelliaDeviceRepairs:camellia_device_repair:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.camelliaDeviceRepairService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "设备维修表-通过id查询")
	@ApiOperation(value="设备维修表-通过id查询", notes="设备维修表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<CamelliaDeviceRepair> queryById(@RequestParam(name="id",required=true) String id) {
		CamelliaDeviceRepair camelliaDeviceRepair = camelliaDeviceRepairService.getById(id);
		if(camelliaDeviceRepair==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(camelliaDeviceRepair);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param camelliaDeviceRepair
	 */
	//@RequiresPermissions("camelliaDeviceRepairs:camellia_device_repair:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, CamelliaDeviceRepair camelliaDeviceRepair) {
		return super.exportXls(request, camelliaDeviceRepair, CamelliaDeviceRepair.class, "设备维修表");
	}

	/**
	 * 通过excel导入数据
	 *
	 * @param request
	 * @param response
	 * @return
	 */
	//@RequiresPermissions("camelliaDeviceRepairs:camellia_device_repair:importExcel")
	@RequestMapping(value = "/importExcel", method = RequestMethod.POST)
	public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
		return super.importExcel(request, response, CamelliaDeviceRepair.class);
	}


	@ApiOperation(value="查询维修状态", notes="查询维修状态")
	@GetMapping(value = "/selRepairList")
	public Result<Object> Test(@RequestParam(name="id",required=true) String id) {
		Map<String,Object> map = new HashMap<>();

		//通过维修id 查询维修信息
		CamelliaDeviceRepair camelliaDeviceRepair = camelliaDeviceRepairService.getById(id);
		if(camelliaDeviceRepair==null) {
			return Result.error("未找到对应数据");
		}
		//查询报修信息
		CamelliaDeviceReport camelliaDeviceReport = camelliaDeviceReportService.selCode(camelliaDeviceRepair.getReportCode());
		if(camelliaDeviceReport==null) {
			return Result.error("未找到对应数据");
		}

		map.put("statue",camelliaDeviceRepair.getState());
		map.put("report",camelliaDeviceReport);
		map.put("user",camelliaDeviceRepairService.selUserList(camelliaDeviceRepair.getRepairCode()));
		map.put("repair",camelliaDeviceRepair);

		return Result.OK(map);
	}



}
